package com.example.administrator.im.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import com.example.administrator.im.MyApplication;
import com.example.administrator.im.utils.DisplayUtils;

import static java.lang.Thread.sleep;

/**
 * create:2018/9/16 8:48
 * author: 姚鸿杰
 **/
public class IM_RecyclerView extends RecyclerView {

    private final String TAG = "IM_RecyclerView";



    //布局管理器
    private LinearLayoutManager manager;

    boolean isRefresh = false;

    public IM_RecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (manager == null) {
                    manager = (LinearLayoutManager) getLayoutManager();
                    ViewGroup.LayoutParams lp = getLayoutParams();
                    marginTop = ((MarginLayoutParams) lp).topMargin -
                            DisplayUtils.dip2px(MyApplication.getmContext(), 1);
                }
            }
        });
    }

    private final int REFRESH_TIME = 400;   //滑动时间
    private final int REFRESH_MORNAL = 0; //正常状态
    private final int REFRESH_DOWN = 1;   //正在下拉
    private final int REFRESH_UP = 2;   //正在下拉
    private final int REFRESH_NOWING = 3; //正在刷新状态

    private int state = REFRESH_MORNAL;     //当前状态
    private boolean isOpenMenu = false;

    private int marginTop;  //隐藏头部的margin值
    private float y, yDown;

    private int top = 0;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        if (state == REFRESH_NOWING) {
            return false;
        }

        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:

                //当前顶部必须是item必须是头布局，并且是下拉，还有不在刷新状态
                if (!this.canScrollVertically(-1) && state != REFRESH_NOWING && state != REFRESH_UP && isRefresh
                        && !isOpenMenu && !IM_Item.isIs_open() && yDown - ev.getY() < 0) {
                    float yMove = ev.getRawY() - y;
                    int top = marginTop + (int) yMove / 3;
                    this.top = top;
                    TranslateAnimation ta = new TranslateAnimation(0, 0, top - marginTop, top - marginTop);
                    ta.setFillAfter(true);
                    startAnimation(ta);

                    state = REFRESH_DOWN;   //将状态设置为下拉
                    return true;
                }

                if (!this.canScrollVertically(1) && state != REFRESH_NOWING && state != REFRESH_DOWN && isRefresh
                        && !isOpenMenu && !IM_Item.isIs_open() && yDown - ev.getY() > 0) {
                    float yMove = y - ev.getRawY();
                    int top = (int) yMove / 3;
                    this.top = top;
                    TranslateAnimation ta = new TranslateAnimation(0, 0, -top, -top);
                    ta.setFillAfter(true);
                    startAnimation(ta);

                    state = REFRESH_UP;   //将状态设置为上拉
                    return true;
                }
                if (!canScrollVertically(1) && !canScrollVertically(-1))
                    state = REFRESH_MORNAL;
                break;

            case MotionEvent.ACTION_UP:

                //判断是否要刷新
                if (state == REFRESH_DOWN) {

                    if (Math.abs(marginTop) / 2 > Math.abs(top - marginTop)) {
                        translate(top - marginTop);
                    } else {
                        final TranslateAnimation ta = new TranslateAnimation(0, 0, top - marginTop, -marginTop);
                        ta.setDuration(REFRESH_TIME);
                        ta.setFillAfter(true);
                        ta.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                                state = REFRESH_NOWING;
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        translate(-marginTop);
                                    }
                                }, 1000);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        startAnimation(ta);
                    }
                }
                if (state == REFRESH_UP) {
                    translate(-top);
                }
                break;
        }

        return super.onTouchEvent(ev);
    }


    private void translate(int offset) {
        //如果要刷新执行
        TranslateAnimation ta = new TranslateAnimation(0, 0, offset, 0);
        ta.setDuration(REFRESH_TIME);
        ta.setFillAfter(true);
        ta.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                state = REFRESH_NOWING;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                state = REFRESH_MORNAL;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        startAnimation(ta);
    }


    private float xLast, yLast;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isRefresh = false;
                isOpenMenu = false;

                if ((!this.canScrollVertically(-1) ||
                        !this.canScrollVertically(1)) &&
                        state != REFRESH_NOWING)
                    isRefresh = true;
                yDown = ev.getY();
                y = ev.getRawY();

                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:

                final float curX = ev.getX() - xLast;
                final float curY = ev.getY() - yLast;

                if (Math.abs(curX) < Math.abs(curY)) {
                    return true;
                } else if (Math.abs(curX) > Math.abs(curY)) {
                    isOpenMenu = true;
                    isRefresh = false;
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
}
